
package org.winexpress

import org.codehaus.groovy.grails.web.json.JSONObject;
import org.codehaus.groovy.grails.web.json.JSONArray;
import grails.converters.JSON

/**
 *
 * @author Swee Zhi
 */
class CustomerManagement {
	
    def listCustomers() {
        def Customers = Customer.list()
        JSONArray arr = new JSONArray()
        Customers.each() {
            JSONObject obj = new JSONObject()
            obj.put("id",it.id)
            obj.put("username",it.username)
            obj.put("password",it.password)
            obj.put("lastLogin",it.lastLogin)
            obj.put("activationStatus",it.activationStatus)
            obj.put("lastName",it.lastName)
            obj.put("firstName",it.firstName)
            obj.put("birthdate",it.birthdate)
            obj.put("blkHouseNo",it.blkHouseNo)
            obj.put("streetName",it.streetName)
            obj.put("postalCode",it.postalCode)
            obj.put("telNo",it.telNo)
            obj.put("countryId",it.countryId)
            arr.add(obj)
        }
        
        return arr
    }
    
    def getCustomerByID(id) {
        def Customer = Customer.findById(id)
        JSONObject obj = new JSONObject()
        obj.put("id",Customer.id)
        obj.put("username",Customer.username)
        obj.put("password",Customer.password)
        obj.put("lastLogin",Customer.lastLogin)
        obj.put("activationStatus",Customer.activationStatus)
        obj.put("lastName",Customer.lastName)
        obj.put("firstName",Customer.firstName)
        obj.put("birthdate",Customer.birthdate)
        obj.put("blkHouseNo",Customer.blkHouseNo)
        obj.put("streetName",Customer.streetName)
        obj.put("postalCode",Customer.postalCode)
        obj.put("telNo",Customer.telNo)
        obj.put("countryId",Customer.countryId)

        return obj
    }
    
    def getCustomerByUsername(username) {
        def Customer = Customer.findByUsername(username)
        JSONObject obj = new JSONObject()
        obj.put("id",Customer.id)
        obj.put("username",Customer.username)
        obj.put("password",Customer.password)
        obj.put("lastLogin",Customer.lastLogin)
        obj.put("activationStatus",Customer.activationStatus)
        obj.put("lastName",Customer.lastName)
        obj.put("firstName",Customer.firstName)
        obj.put("birthdate",Customer.birthdate)
        obj.put("blkHouseNo",Customer.blkHouseNo)
        obj.put("streetName",Customer.streetName)
        obj.put("postalCode",Customer.postalCode)
        obj.put("telNo",Customer.telNo)
        obj.put("countryId",Customer.countryId)
        
        return obj
    }
    
    def addCustomer(o) {
        def status
        
        Customer c = new Customer()
        c.username = o.get("username")
        c.password = o.get("password")
        c.activationStatus = false
        // my null for lastlogin does not work!
        Date d = new Date();
        c.lastLogin = d
        
        c.lastName = o.get("lastName")
        c.firstName = o.get("firstName")
        c.birthdate = o.get("birthdate")
        
        c.blkHouseNo = o.get("blkHouseNo")
        c.streetName = o.get("streetName")
        c.postalCode = o.get("postalCode")
        
        CountryManagement ctyman = new CountryManagement()
        def cty = ctyman.getCountryByCountryName(o.get("country"))
        c.countryId = cty.id
        
        c.telNo = o.get("telNo")
        
        if (c.save(flush:true)) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Add customer >> ' + status
        
        return status
    }
    
    def updateCustomer(customer) {
        def status
        
        Customer c = Customer.findById(customer.id)
        if (customer.has("password")) { c.password = customer.get("password"); println c.password }
        if (customer.has("activationStatus")) { c.activationStatus = customer.get("activationStatus"); println c.activationStatus }
        if (customer.has("blkHouseNo")) { c.blkHouseNo = customer.get("blkHouseNo"); println c.blkHouseNo }
        if (customer.has("streetName")) { c.streetName = customer.get("streetName"); println c.streetName }
        if (customer.has("countryId")) { c.countryId = customer.get("countryId"); println c.countryId }
        if (customer.has("postalCode")) { c.postalCode = customer.get("postalCode"); println c.postalCode }
        if (customer.has("telNo")) { c.telNo = customer.get("telNo"); println c.telNo  }
        Date d = new Date();
        c.lastLogin = d
        
        if (c.save(flush:true)) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Update customer >> ' + status
        
        return status
    }
    
    def deleteCustomer(id) {
        def status
        
        Customer c = Customer.findById(id)
        if (c.delete()) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Delete customer >> ' + status
        
        return status
    }
}

